package com.mirai.mirai_boot.service.impl;

import catcode.CatCodeUtil;
import catcode.Neko;
import com.mirai.mirai_boot.api.KuGouMusicApi;
import com.mirai.mirai_boot.api.NeteaseCloudMusicApi;
import com.mirai.mirai_boot.entity.Music;
import com.mirai.mirai_boot.service.MusicService;
import com.mirai.mirai_boot.utils.CutStringUtils;
import love.forte.simbot.api.message.events.GroupMsg;
import love.forte.simbot.api.sender.Sender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author : CP
 * @version: 1.0
 * @program : mirai_boot
 * @description : 实现点歌
 * @date : 2021-07-01 14:58
 **/
@Service
public class PickMusicImpl implements MusicService {

    @Autowired
    private NeteaseCloudMusicApi neteaseCloudMusicApi;
    @Autowired
    private KuGouMusicApi kuGouMusicApi;

    @Override
    public void pickMusic(String para, GroupMsg msg, Sender sender) throws IOException {

        if(para!=null){
            CutStringUtils cut = new CutStringUtils(para);
            // 神奇的喵喵码
            CatCodeUtil util = CatCodeUtil.INSTANCE;


            Music music = null;
            if (cut.getPara() != null) {
                switch (cut.getType()) {
                    case "网易":
                        music = neteaseCloudMusicApi.getMusic(cut.getPara());
                        break;
                    case "qq":
                    case "QQ":
                        music = kuGouMusicApi.getMusic(cut.getPara());
                        break;
                    default:
                        break;
                }
            } else {
                music = neteaseCloudMusicApi.getMusic(cut.getType());
            }

            // 处理结果
            if (music == null || music.getMusicUrl().equals("")) {
                sender.sendGroupMsg(msg, "亲，不好意思，该歌曲为付费歌曲~");
            } else {

                Map<String, String> map = new HashMap<>();
                map.put("type", music.getType());
                map.put("content", music.getSummary());
                map.put("title", music.getTitle());
                map.put("pictureUrl",music.getPictureUrl());
                map.put("jumpUrl", music.getJumpUrl());
                map.put("musicUrl", music.getMusicUrl());
                map.put("brief", music.getBrief());
                Neko neko = util.toNeko("music", map);

                sender.sendGroupMsg(msg, neko.toString());
            }
        }else {
            sender.sendGroupMsg(msg,"点歌 <您想听的音乐>");
        }

    }
}
